package com.fuint.module.backendApi.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fuint.common.service.WeixinService;
import com.fuint.framework.exception.BusinessCheckException;
import com.fuint.framework.web.BaseController;
import com.fuint.framework.web.ResponseObject;
import com.fuint.repository.mapper.MtMessageMapper;
import com.fuint.repository.model.MtMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import weixin.popular.util.JsonUtil;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Api(tags = "管理端-测试接口")
@RestController
@AllArgsConstructor
@RequestMapping(value = "/backendApi/test")
public class BackendTestController extends BaseController {

    @Autowired
    private MtMessageMapper mtMessageMapper;

    @Autowired
    private WeixinService weixinService;

    @ApiOperation(value = "消息列表")
    @RequestMapping(value = "/message/list", method = RequestMethod.GET)
    @CrossOrigin
    public ResponseObject list(String isSend, Integer id) throws BusinessCheckException {
        LambdaQueryWrapper<MtMessage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StrUtil.isNotEmpty(isSend), MtMessage::getIsSend, isSend);
        wrapper.eq(Objects.nonNull(id), MtMessage::getId, id);
        List<MtMessage> needSendMessage = mtMessageMapper.selectList(wrapper);
        return getSuccessResult(needSendMessage);
    }

    @ApiOperation(value = "消息发送")
    @RequestMapping(value = "/message/send", method = RequestMethod.GET)
    @CrossOrigin
    public ResponseObject send(@RequestParam Integer id) throws BusinessCheckException {
        MtMessage mtMessage = mtMessageMapper.selectById(id);
        String params = mtMessage.getParams();
        boolean result = weixinService.doSendSubscribeMessage(0, params);
        return getSuccessResult(result);
    }
}
